'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 3_study_react_for_google_serper.py
* @Time: 2025/11/5
* @All Rights Reserve By Brtc
'''
import dotenv
from langchain import hub
from langchain.agents import create_react_agent, AgentExecutor
from langchain_community.tools import GoogleSerperRun
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_core.tools import render_text_description_and_args
from langchain_openai import ChatOpenAI

from pydantic import BaseModel, Field

dotenv.load_dotenv()
class GoogleSerperSchema(BaseModel):
    query:str = Field(description="执行谷歌搜索的查询语句")

google_serper = GoogleSerperRun(
    name = "google_serper",
    description = (
       "一个低成本的谷歌搜索工具"
       "当你想要回答有关问题的时候,可以调用该工具"
       "该工具的输入是搜索查询语句"),
        api_wrapper=GoogleSerperAPIWrapper()
)
tools = [google_serper]

#2、定义智能体提示模板
prompt = hub.pull("hwchase17/react")

#3、创建大预言模型
llm = ChatOpenAI(model="gpt-4o-mini")
agent = create_react_agent(
    llm=llm,
    tools=tools,
    prompt=prompt,
    tools_renderer=render_text_description_and_args
)

agent_executer = AgentExecutor(agent=agent, tools=tools, verbose=True)

print(agent_executer.invoke({"input":"你好"}))
